package co.yixiang.modules.quartz.task;

import co.yixiang.modules.shop.domain.YxSalesCommission;
import co.yixiang.modules.shop.service.YxInvestOrderService;
import co.yixiang.modules.shop.service.YxSalesCommissionService;
import co.yixiang.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 统计业务员提成定时器
 *
 * @author frq
 * @date 2020-6-11
 */
@Slf4j
@Component
public class SalesmanTimer {

    @Autowired
    private YxSalesCommissionService salesCommissionService;

    @Autowired
    private YxInvestOrderService investOrderService;

    /**
     * 统计业务员提成并入库
     * @throws InterruptedException
     */
    public void run()throws InterruptedException{
       List<Map<String,Object>> listMap=investOrderService.findSalesCommission();
        for (Map<String,Object> map:listMap) {
            YxSalesCommission salesCommission=new YxSalesCommission();
            salesCommission.setMonth(map.get("month").toString());
            salesCommission.setSalesName(map.get("salesName").toString());
            salesCommission.setSalesAvatar(map.get("salesAvatar").toString());
            salesCommission.setSalesPhone(map.get("salesPhone").toString());
            salesCommission.setAddTime(new Date());
            if(StringUtils.isNotEmpty(map.get("commission").toString())){
                BigDecimal big=new BigDecimal(map.get("commission").toString());
                salesCommission.setCommission(big);
            }
            if(StringUtils.isNotEmpty(map.get("orderCount").toString())){
                salesCommission.setOrderCount(Integer.parseInt(map.get("orderCount").toString()));
            }
            salesCommissionService.create(salesCommission);
        }
    }
}
